package cn.com.lms.business.livecourse.web;

import cn.com.lms.business.core.util.CurrentUserInstance;
import cn.com.lms.business.course.service.FbCourseInfoService;
import cn.com.lms.business.livecourse.persistence.entity.vo.CourseLiveSignInVo;
import cn.com.lms.business.livecourse.persistence.entity.vo.CourseLiveSignOutVo;
import cn.com.lms.business.livecourse.service.CourseLiveService;
import cn.com.lms.business.livecourse.service.CourseLiveSignService;
import cn.com.lms.business.user.persistence.entity.SysUserVo;

import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.rest.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * LiveCouserAppController
 *
 * @author barry
 * @date 2020/3/31
 **/
@Tag(name = "课程相关", description = "直播课程")
@RestController
@RequestMapping("/app/course/live")
public class LiveCourseAppController {

    @Autowired
    CourseLiveService courseLiveService;

    @Autowired
    private CourseLiveSignService courseLiveSignService;

    @GetMapping("/courseList")
    @Operation(summary = "我的直播课程列表(待直播和正在直播;已录制完成和录制生成中)", parameters = {
            @Parameter(name = "liveStatusType", description = "直播课程类型： 1 待直播和正在直播；2已录制完成（可回看)", allowReserved = true, schema = @Schema(type = "int", description = "直播状态类型"), required = true),
            //* @param liveStatusType  直播课程类型： 1 待直播和正在直播；2已录制完成（可回看)
            @Parameter(name = "title", description = "课程标题", allowReserved = true, schema = @Schema(type = "String", description = "课程标题"), required = false),
            @Parameter(name = "limit", description = "分页大小", allowReserved = true, schema = @Schema(type = "int", description = "分页大小")),
            @Parameter(name = "offset", description = "页码", allowReserved = true, schema = @Schema(type = "int", description = "页码"))})
    public ResponseResult courseList(HttpServletRequest request) {
        return courseLiveService.courseList(request);
    }


    @Operation(summary = "直播/视频课程 学生签到",
            parameters = {
                @Parameter(name = "courseId", description = "课程id", required = true, schema = @Schema(implementation = Long.class)),
                    @Parameter(name = "planId", description = "学习计划id", required = true, schema = @Schema(implementation = Long.class)),
                    @Parameter(name = "stageDetailId", description = "学习阶段详情id", required = true, schema = @Schema(implementation = Long.class))
    })
    @GetMapping("/signIn")
    public ResponseResult<CourseLiveSignInVo> liveCourseSignIn(long courseId, long planId, long stageDetailId) {
        CourseLiveSignInVo courseLiveSignInVo = null;
        try {
            courseLiveSignInVo = courseLiveSignService.liveCourseSignIn(courseId, planId, stageDetailId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.bizException(new BusinessException(e.getMessage(), String.valueOf(ResultCode.FAILURE)));
        }
        return ResponseResult.success(courseLiveSignInVo);
    }


    @Operation(summary = "直播/视频课程 学生签出", parameters = {
            @Parameter(name = "signId", description = "签到记录id", required = true, schema = @Schema(implementation = Long.class)),
            @Parameter(name = "duration", description = "本次上报的学习时长（单位分钟）", required = true, schema = @Schema(implementation = Integer.class)),
            @Parameter(name = "finishState", description = "学习完成状态：0未完成；1完成", required = true, schema = @Schema(implementation = Integer.class))
    })
    @GetMapping("/signOut")
    public ResponseResult<CourseLiveSignOutVo> liveCourseSignIn(long signId, int duration, int finishState) {
        SysUserVo user = CurrentUserInstance.getCurrentUser();
        if (null == user || null == user.getId() || 0 == user.getId()) {
            return ResponseResult.bizException(new BusinessException("用户信息异常", String.valueOf(ResultCode.FAILURE)));
        }
        try {
            // 签出处理
            long respSignId = courseLiveSignService.liveCourseSignOut(signId, duration, finishState);
            CourseLiveSignOutVo vo = new CourseLiveSignOutVo();
            vo.setRespSignId(respSignId);
            return ResponseResult.success(vo);

        } catch (Exception e) {
//            e.printStackTrace();
            return ResponseResult.bizException(new BusinessException(e.getMessage(), String.valueOf(ResultCode.FAILURE)));
        }
    }
}
